package repository

import (
	"context"
	"database/sql"
	"fmt"
	"strings"

	"mall/common/enumx"
	"mall/common/modelx"
	"mall/service/blog/model"

	"github.com/volatiletech/sqlboiler/v4/boil"
	"github.com/volatiletech/sqlboiler/v4/queries/qm"
	"github.com/zeromicro/go-zero/core/stores/cache"
)

type (
	// BlogPageRepository is an interface to be customized, add more methods here,
	// and implement the added methods in customBlogPageRepository.
	BlogPageRepository interface {
		Conn() error
		FindOne(ctx context.Context, id int64, cols []string) (*model.BlogPage, error)
		GetList(
			ctx context.Context,
			cols model.M,
			withCount int,
			filter map[string]map[string][]any,
		) ([]*model.BlogPage, int64, error)
		Insert(
			ctx context.Context,
			page *model.BlogPage,
			detail *model.BlogPageDetail,
			meta []*model.BlogPageMetum,
			extra *model.BlogExtra,
		) error
		Update(
			ctx context.Context,
			page *model.BlogPage,
			detail *model.BlogPageDetail,
			meta []*model.BlogPageMetum,
			extra *model.BlogExtra,
		) (int64, error)
		UpdateBatch(ctx context.Context, ids []int64, cols model.M) (int64, error)  //////
		ForceDeleteBatch(ctx context.Context, ids []int64) (int64, error)  //////
		GetDetail(
			ctx context.Context,
			cols model.M,
			withCount int,
			filter map[string]map[string][]any,
		) ([]*model.BlogPageDetail, int64, error)
		GetMeta(
			ctx context.Context,
			cols model.M,
			withCount int,
			filter map[string]map[string][]any,
		) ([]*model.BlogPageMetum, int64, error)
		GetMedia(
			ctx context.Context,
			cols model.M,
			withCount int,
			filter map[string]map[string][]any,
		) ([]*model.BlogAttachmentRelationship, int64, error)
	}

	customBlogPageRepository struct {
		cacheConf  cache.CacheConf
		dataSource string

		mysqlConn *sql.DB
	}
)

// NewBlogPageRepository returns a repository for the database table.
func NewBlogPageRepository(dataSource string, cacheConf cache.CacheConf) BlogPageRepository {
	return &customBlogPageRepository{
		cacheConf:  cacheConf,
		dataSource: dataSource,
	}
}

func (r *customBlogPageRepository) Conn() error {
	if r.mysqlConn == nil {
		mysqlConn, err := modelx.NewMysqlConn(r.dataSource)
		if err != nil {
			return err
		}

		r.mysqlConn = mysqlConn
	}

	return nil
}

func (r *customBlogPageRepository) FindOne(
	ctx context.Context,
	id int64,
	cols []string,
) (page *model.BlogPage, err error) {
	if err = r.Conn(); err != nil {
		return
	}

	page, err = model.FindBlogPage(ctx, r.mysqlConn, uint64(id), cols...)
	if err == sql.ErrNoRows {
		return nil, model.ErrNotFound
	}

	return
}

func (r *customBlogPageRepository) GetList(
	ctx context.Context,
	cols model.M,
	withCount int,
	filter map[string]map[string][]any,
) (list []*model.BlogPage, count int64, err error) {
	if err = r.Conn(); err != nil {
		return
	}

	var mods []qm.QueryMod
	var page int
	var pageSize int
	var offset int
	var limit int
	var selectCol string
	for k, v := range cols {
		switch k {
		case "page":
			if val, ok := v.(int64); ok && val > 1 {
				page = int(val)
			}
		case "pageSize":
			if val, ok := v.(int64); ok && val > 0 {
				pageSize = int(val)
			}
		case "offset":
			if val, ok := v.(int32); ok && val > 0 {
				offset = int(val)
			}
		case "limit":
			if val, ok := v.(int32); ok && val > 0 {
				limit = int(val)
			}
		case "orderBy":
			if val, ok := v.(string); ok && val != "" {
				mods = append(mods, qm.OrderBy(val))
			}
		case "select":
			if val, ok := v.(string); ok && val != "" {
				selectCol = val
			}
		case "asPreview":
			if val, ok := v.(string); ok && val == "" { // 不是预览，必须是已发布
				mods = append(mods, model.BlogPageWhere.PostStatus.EQ(uint16(enumx.PostStatusPublish)))
			}
		case "search":
			if val, ok := v.(string); ok && val != "" { // 搜索
				likeVal := fmt.Sprintf("%%%s%%", val)
				mods = append(mods, qm.Where(fmt.Sprintf(
					"(%s LIKE ? OR %s LIKE ?)",
					model.BlogPageColumns.PostTitle,
					model.BlogPageColumns.PostName), likeVal, likeVal))
			}
		}
	}

	if filter != nil {
		for key, opMap := range filter {
			switch key {
			case model.BlogPageColumns.ID:
				mods = append(mods, modelx.GetIntMods(model.BlogPageColumns.ID, opMap)...)
			case model.BlogPageColumns.PostAuthorID:
				mods = append(mods, modelx.GetIntMods(model.BlogPageColumns.PostAuthorID, opMap)...)
			case model.BlogPageColumns.PostParentID:
				mods = append(mods, modelx.GetIntMods(model.BlogPageColumns.PostParentID, opMap)...)
			case model.BlogPageColumns.PostTitle:
				mods = append(mods, modelx.GetStringMods(model.BlogPageColumns.PostTitle, opMap)...)
			case model.BlogPageColumns.PostExcerpt:
				mods = append(mods, modelx.GetStringMods(model.BlogPageColumns.PostExcerpt, opMap)...)
			case model.BlogPageColumns.PostStatus:
				mods = append(mods, modelx.GetIntMods(model.BlogPageColumns.PostStatus, opMap)...)
			case model.BlogPageColumns.CommentStatus:
				mods = append(mods, modelx.GetIntMods(model.BlogPageColumns.CommentStatus, opMap)...)
			case model.BlogPageColumns.PostName:
				mods = append(mods, modelx.GetStringMods(model.BlogPageColumns.PostName, opMap)...)
			}
		}
	}

	if withCount >= 0 {
		if withCount >= 2 {
			var nextCount int64
			var previousCount int64
			subMods := make([]qm.QueryMod, len(mods), len(mods) + 3)
			copy(subMods, mods)
			subMods = append(subMods, qm.Limit(1))
			subMods = append(subMods, qm.Select(model.BlogPageColumns.ID))

			if withCount != 3 && offset >= 1 {
				subMods = append(subMods, qm.Offset(offset - 1))
				previousList, previousErr := model.BlogPages(subMods...).All(ctx, r.mysqlConn)
				if previousErr != nil {
					err = previousErr
					return
				}
				if len(previousList) > 0 {
					previousCount = 1
				}
			}
			if limit > 0 {
				subMods = append(subMods, qm.Offset(offset + limit))
				nextList, nextErr := model.BlogPages(subMods...).All(ctx, r.mysqlConn)
				if nextErr != nil {
					err = nextErr
					return
				}
				if len(nextList) > 0 {
					nextCount = 1
				}
			}

			count = previousCount + nextCount * 2
		} else {
			count, err = model.BlogPages(mods...).Count(ctx, r.mysqlConn)
			if err != nil {
				return
			}

			if withCount == 0 {
				return
			}
		}
	}

	if pageSize > 0 {
		if page > 1 {
			mods = append(mods, qm.Offset(pageSize*(page-1)))
		}
		mods = append(mods, qm.Limit(pageSize))
	} else if limit > 0 || offset > 0 {
		if limit > 0 {
			mods = append(mods, qm.Limit(limit))
		}
		if offset > 0 {
			mods = append(mods, qm.Offset(offset))
		}
	}

	var columns []string
	if selectCol != "" {
		if selectCol != "," {
			columns = strings.Split(selectCol, ",")
		}
		columns = append(columns, model.BlogPageColumns.ID)
	}
	if len(columns) > 0 {
		mods = append(mods, qm.Select(columns...))
	}

	list, err = model.BlogPages(mods...).All(ctx, r.mysqlConn)
	return
}

func (r *customBlogPageRepository) Insert(
	ctx context.Context,
	page *model.BlogPage,
	detail *model.BlogPageDetail,
	meta []*model.BlogPageMetum,
	extra *model.BlogExtra,
) error {
	if err := r.Conn(); err != nil {
		return err
	}

	tx, err := r.mysqlConn.BeginTx(ctx, nil)
	if err != nil {
		return err
	}

	err = page.Insert(ctx, tx, boil.Infer())
	if err != nil {
		tx.Rollback()
		return err
	}

	pid := page.ID
	detail.PageID = pid
	err = detail.Insert(ctx, tx, boil.Infer())
	if err != nil {
		tx.Rollback()
		return err
	}

	if len(meta) > 0 {
		for _, m := range meta {
			m.PageID = pid
			err = m.Insert(ctx, tx, boil.Infer())
			if err != nil {
				tx.Rollback()
				return err
			}
		}
	}

	objectType := uint16(enumx.PostTypePage)
	if len(extra.CategoryIds) > 0 {
		for _, cid := range extra.CategoryIds {
			var relationShip model.BlogTermRelationship
			relationShip.ObjectID = pid
			relationShip.ObjectType = objectType
			relationShip.TermID = uint64(cid)
			err = relationShip.Insert(ctx, tx, boil.Infer())
			if err != nil {
				tx.Rollback()
				return err
			}
		}
	}
	if len(extra.TagIds) > 0 {
		for _, tid := range extra.TagIds {
			var relationShip model.BlogTermRelationship
			relationShip.ObjectID = pid
			relationShip.ObjectType = objectType
			relationShip.TermID = uint64(tid)
			err = relationShip.Insert(ctx, tx, boil.Infer())
			if err != nil {
				tx.Rollback()
				return err
			}
		}
	}
	if len(extra.Media) > 0 {
		for _, m := range extra.Media {
			m.ObjectID = pid
			m.ObjectType = objectType
			err = m.Insert(ctx, tx, boil.Infer())
			if err != nil {
				tx.Rollback()
				return err
			}
		}
	}

	tx.Commit()
	return nil
}

func (r *customBlogPageRepository) Update(
	ctx context.Context,
	page *model.BlogPage,
	detail *model.BlogPageDetail,
	meta []*model.BlogPageMetum,
	extra *model.BlogExtra,
) (int64, error) {
	if err := r.Conn(); err != nil {
		return 0, err
	}

	tx, err := r.mysqlConn.BeginTx(ctx, nil)
	if err != nil {
		return 0, err
	}

	pageId, err := page.Update(ctx, tx, boil.Blacklist(
		model.BlogPageColumns.PostAuthorID,
		model.BlogPageColumns.PostParentID,
		model.BlogPageColumns.DelStatus,
		model.BlogPageColumns.CommentCount,
	))
	if err != nil {
		tx.Rollback()
		return 0, err
	}

	pid := page.ID
	detail.PageID = pid
	_, err = detail.Update(ctx, tx, boil.Blacklist(
		model.BlogPageDetailColumns.DetailID,
		model.BlogPageDetailColumns.PageID,
		model.BlogPageDetailColumns.PingStatus,
		model.BlogPageDetailColumns.ToPing,
		model.BlogPageDetailColumns.Pinged,
	))
	if err != nil {
		tx.Rollback()
		return 0, err
	}

	if len(meta) > 0 {
		for _, m := range meta {
			if m.MetaID > 0 {
				_, err = m.Update(ctx, tx, boil.Blacklist(
					model.BlogPageMetumColumns.MetaID,
					model.BlogPageMetumColumns.PageID,
				))
			} else {
				m.PageID = pid
				err = m.Insert(ctx, tx, boil.Infer())
			}

			if err != nil {
				tx.Rollback()
				return 0, err
			}
		}
	}

	objectType := uint16(enumx.PostTypePage)
	if len(extra.Terms) > 0 {
		for _, t := range extra.Terms {
			if t.TermRelationshipID > 0 {
				_, err = t.Update(ctx, tx, boil.Blacklist(
					model.BlogTermRelationshipColumns.TermRelationshipID,
					model.BlogTermRelationshipColumns.ObjectID,
					model.BlogTermRelationshipColumns.ObjectType,
					model.BlogTermRelationshipColumns.TermID,
				))
				if err != nil {
					tx.Rollback()
					return 0, err
				}
			}
		}
	}
	if len(extra.CategoryIds) > 0 {
		for _, cid := range extra.CategoryIds {
			var relationShip model.BlogTermRelationship
			relationShip.ObjectID = pid
			relationShip.ObjectType = objectType
			relationShip.TermID = uint64(cid)
			err = relationShip.Insert(ctx, tx, boil.Infer())
			if err != nil {
				tx.Rollback()
				return 0, err
			}
		}
	}
	if len(extra.TagIds) > 0 {
		for _, tid := range extra.TagIds {
			var relationShip model.BlogTermRelationship
			relationShip.ObjectID = pid
			relationShip.ObjectType = objectType
			relationShip.TermID = uint64(tid)
			err = relationShip.Insert(ctx, tx, boil.Infer())
			if err != nil {
				tx.Rollback()
				return 0, err
			}
		}
	}
	if len(extra.Media) > 0 {
		for _, m := range extra.Media {
			if m.AttachmentRelationshipID > 0 {
				_, err = m.Update(ctx, tx, boil.Blacklist(
					model.BlogAttachmentRelationshipColumns.AttachmentRelationshipID,
					model.BlogAttachmentRelationshipColumns.ObjectID,
					model.BlogAttachmentRelationshipColumns.ObjectType,
					model.BlogAttachmentRelationshipColumns.AttachmentID,
				))
			} else {
				m.ObjectID = pid
				m.ObjectType = objectType
				err = m.Insert(ctx, tx, boil.Infer())
			}
			if err != nil {
				tx.Rollback()
				return 0, err
			}
		}
	}
	if len(extra.DelMetaIds) > 0 {
		ids := make([]any, 0, len(extra.DelMetaIds))
		for _, id := range extra.DelMetaIds {
			ids = append(ids, id)
		}
		var mods []qm.QueryMod
		mods = append(mods, qm.WhereIn(fmt.Sprintf("%s IN ?", model.BlogPageMetumColumns.MetaID), ids...))
		_, err := model.BlogPageMeta(mods...).DeleteAll(ctx, tx)
		if err != nil {
			tx.Rollback()
			return 0, err
		}
	}
	if len(extra.DelTermIds) > 0 {
		ids := make([]any, 0, len(extra.DelTermIds))
		for _, id := range extra.DelTermIds {
			ids = append(ids, id)
		}
		var mods []qm.QueryMod
		mods = append(mods, qm.WhereIn(fmt.Sprintf("%s IN ?", model.BlogTermRelationshipColumns.TermRelationshipID), ids...))
		_, err := model.BlogTermRelationships(mods...).DeleteAll(ctx, tx)
		if err != nil {
			tx.Rollback()
			return 0, err
		}
	}
	if len(extra.DelMediaIds) > 0 {
		ids := make([]any, 0, len(extra.DelMediaIds))
		for _, id := range extra.DelMediaIds {
			ids = append(ids, id)
		}
		var mods []qm.QueryMod
		mods = append(mods, qm.WhereIn(fmt.Sprintf("%s IN ?", model.BlogAttachmentRelationshipColumns.AttachmentRelationshipID), ids...))
		_, err := model.BlogAttachmentRelationships(mods...).DeleteAll(ctx, tx)
		if err != nil {
			tx.Rollback()
			return 0, err
		}
	}

	tx.Commit()
	return pageId, nil
}

func (r *customBlogPageRepository) UpdateBatch(ctx context.Context, ids []int64, cols model.M) (rowsAff int64, err error) {
	if err = r.Conn(); err != nil {
		return
	}

	var mods []qm.QueryMod
	if len(ids) > 0 {
		values := make([]any, 0, len(ids))
		for _, id := range ids {
			values = append(values, uint64(id))
		}

		mods = append(mods, qm.WhereIn(fmt.Sprintf("%s IN ?", model.BlogPageColumns.ID), values...))
	}

	rowsAff, err = model.BlogPages(mods...).UpdateAll(ctx, r.mysqlConn, cols)
	return
}

func (r *customBlogPageRepository) ForceDeleteBatch(ctx context.Context, ids []int64) (rowsAff int64, err error) {
	if err = r.Conn(); err != nil {
		return
	}

	tx, err := r.mysqlConn.BeginTx(ctx, nil)
	if err != nil {
		return 0, err
	}

	if len(ids) > 0 {
		var mods []qm.QueryMod
		values := make([]any, 0, len(ids))
		for _, id := range ids {
			values = append(values, uint64(id))
		}
		mods = append(mods, qm.WhereIn(fmt.Sprintf("%s IN ?", model.BlogPageColumns.ID), values...))
		rowsAff, err = model.BlogPages(mods...).DeleteAll(ctx, tx)
		if err != nil {
			tx.Rollback()
			return 0, err
		}

		mod := qm.WhereIn(fmt.Sprintf("%s IN ?", model.BlogPageDetailColumns.PageID), values...)
		_, err = model.BlogPageDetails(mod).DeleteAll(ctx, tx)
		if err != nil {
			tx.Rollback()
			return 0, err
		}

		mod = qm.WhereIn(fmt.Sprintf("%s IN ?", model.BlogPageMetumColumns.PageID), values...)
		_, err = model.BlogPageMeta(mod).DeleteAll(ctx, tx)
		if err != nil {
			tx.Rollback()
			return 0, err
		}

		var termRelationshipMods []qm.QueryMod
		termRelationshipMods = append(
			termRelationshipMods,
			qm.WhereIn(fmt.Sprintf("%s IN ?", model.BlogTermRelationshipColumns.ObjectID), values...),
		)
		termRelationshipMods = append(
			termRelationshipMods,
			model.BlogTermRelationshipWhere.ObjectType.EQ(uint16(enumx.PostTypePage)),
		)
		_, err = model.BlogTermRelationships(termRelationshipMods...).DeleteAll(ctx, tx)
		if err != nil {
			tx.Rollback()
			return 0, err
		}

		var attachmentRelationshipMods []qm.QueryMod
		attachmentRelationshipMods = append(
			attachmentRelationshipMods,
			qm.WhereIn(fmt.Sprintf("%s IN ?", model.BlogAttachmentRelationshipColumns.ObjectID), values...),
		)
		attachmentRelationshipMods = append(
			attachmentRelationshipMods,
			model.BlogAttachmentRelationshipWhere.ObjectType.EQ(uint16(enumx.PostTypePage)),
		)
		_, err = model.BlogAttachmentRelationships(attachmentRelationshipMods...).DeleteAll(ctx, tx)
		if err != nil {
			tx.Rollback()
			return 0, err
		}
	}

	tx.Commit()
	return
}

func (r *customBlogPageRepository) GetDetail(
	ctx context.Context,
	cols model.M,
	withCount int,
	filter map[string]map[string][]any,
) (list []*model.BlogPageDetail, count int64, err error) {
	if err = r.Conn(); err != nil {
		return
	}

	var mods []qm.QueryMod
	var page int
	var pageSize int
	var selectCol string
	for k, v := range cols {
		switch k {
		case "page":
			if val, ok := v.(int64); ok && val > 1 {
				page = int(val)
			}
		case "pageSize":
			if val, ok := v.(int64); ok && val > 0 {
				pageSize = int(val)
			}
		case "orderBy":
			if val, ok := v.(string); ok && val != "" {
				mods = append(mods, qm.OrderBy(val))
			}
		case "select":
			if val, ok := v.(string); ok && val != "" {
				selectCol = val
			}
		}
	}

	if filter != nil {
		for key, opMap := range filter {
			switch key {
			case model.BlogPageDetailColumns.DetailID:
				mods = append(mods, modelx.GetIntMods(model.BlogPageDetailColumns.DetailID, opMap)...)
			case model.BlogPageDetailColumns.PageID:
				mods = append(mods, modelx.GetIntMods(model.BlogPageDetailColumns.PageID, opMap)...)
			}
		}
	}

	if withCount >= 0 {
		count, err = model.BlogPageDetails(mods...).Count(ctx, r.mysqlConn)
		if err != nil {
			return
		}

		if withCount == 0 {
			return
		}
	}

	if pageSize > 0 {
		if page > 1 {
			mods = append(mods, qm.Offset(pageSize*(page-1)))
		}
		mods = append(mods, qm.Limit(pageSize))
	}

	var columns []string
	if selectCol != "" {
		if selectCol != "," {
			columns = strings.Split(selectCol, ",")
		}
		columns = append(columns, model.BlogPageDetailColumns.DetailID)
	}
	if len(columns) > 0 {
		mods = append(mods, qm.Select(columns...))
	}

	list, err = model.BlogPageDetails(mods...).All(ctx, r.mysqlConn)
	return
}

func (r *customBlogPageRepository) GetMeta(
	ctx context.Context,
	cols model.M,
	withCount int,
	filter map[string]map[string][]any,
) (list []*model.BlogPageMetum, count int64, err error) {
	if err = r.Conn(); err != nil {
		return
	}

	var mods []qm.QueryMod
	var page int
	var pageSize int
	var selectCol string
	for k, v := range cols {
		switch k {
		case "page":
			if val, ok := v.(int64); ok && val > 1 {
				page = int(val)
			}
		case "pageSize":
			if val, ok := v.(int64); ok && val > 0 {
				pageSize = int(val)
			}
		case "orderBy":
			if val, ok := v.(string); ok && val != "" {
				mods = append(mods, qm.OrderBy(val))
			}
		case "select":
			if val, ok := v.(string); ok && val != "" {
				selectCol = val
			}
		}
	}

	if filter != nil {
		for key, opMap := range filter {
			switch key {
			case model.BlogPageMetumColumns.MetaID:
				mods = append(mods, modelx.GetIntMods(model.BlogPageMetumColumns.MetaID, opMap)...)
			case model.BlogPageMetumColumns.PageID:
				mods = append(mods, modelx.GetIntMods(model.BlogPageMetumColumns.PageID, opMap)...)
			case model.BlogPageMetumColumns.MetaKey:
				mods = append(mods, modelx.GetStringMods(model.BlogPageMetumColumns.MetaKey, opMap)...)
			}
		}
	}

	if withCount >= 0 {
		count, err = model.BlogPageMeta(mods...).Count(ctx, r.mysqlConn)
		if err != nil {
			return
		}

		if withCount == 0 {
			return
		}
	}

	if pageSize > 0 {
		if page > 1 {
			mods = append(mods, qm.Offset(pageSize*(page-1)))
		}
		mods = append(mods, qm.Limit(pageSize))
	}

	var columns []string
	if selectCol != "" {
		if selectCol != "," {
			columns = strings.Split(selectCol, ",")
		}
		columns = append(columns, model.BlogPageMetumColumns.MetaID)
	}
	if len(columns) > 0 {
		mods = append(mods, qm.Select(columns...))
	}

	list, err = model.BlogPageMeta(mods...).All(ctx, r.mysqlConn)
	return
}

func (r *customBlogPageRepository) GetMedia(
	ctx context.Context,
	cols model.M,
	withCount int,
	filter map[string]map[string][]any,
) (list []*model.BlogAttachmentRelationship, count int64, err error) {
	if err = r.Conn(); err != nil {
		return
	}

	var mods []qm.QueryMod
	var page int
	var pageSize int
	var selectCol string
	for k, v := range cols {
		switch k {
		case "page":
			if val, ok := v.(int64); ok && val > 1 {
				page = int(val)
			}
		case "pageSize":
			if val, ok := v.(int64); ok && val > 0 {
				pageSize = int(val)
			}
		case "orderBy":
			if val, ok := v.(string); ok && val != "" {
				mods = append(mods, qm.OrderBy(val))
			}
		case "select":
			if val, ok := v.(string); ok && val != "" {
				selectCol = val
			}
		}
	}

	if filter != nil {
		for key, opMap := range filter {
			switch key {
			case model.BlogAttachmentRelationshipColumns.AttachmentRelationshipID:
				mods = append(mods, modelx.GetIntMods(model.BlogAttachmentRelationshipColumns.AttachmentRelationshipID, opMap)...)
			case model.BlogAttachmentRelationshipColumns.ObjectID:
				mods = append(mods, modelx.GetIntMods(model.BlogAttachmentRelationshipColumns.ObjectID, opMap)...)
			case model.BlogAttachmentRelationshipColumns.ObjectType:
				mods = append(mods, modelx.GetIntMods(model.BlogAttachmentRelationshipColumns.ObjectType, opMap)...)
			case model.BlogAttachmentRelationshipColumns.AttachmentID:
				mods = append(mods, modelx.GetIntMods(model.BlogAttachmentRelationshipColumns.AttachmentID, opMap)...)
			case model.BlogAttachmentRelationshipColumns.AttachmentThumbnail:
				mods = append(mods, modelx.GetIntMods(model.BlogAttachmentRelationshipColumns.AttachmentThumbnail, opMap)...)
			case model.BlogAttachmentRelationshipColumns.AttachmentOrder:
				mods = append(mods, modelx.GetIntMods(model.BlogAttachmentRelationshipColumns.AttachmentOrder, opMap)...)
			}
		}
	}

	if withCount >= 0 {
		count, err = model.BlogAttachmentRelationships(mods...).Count(ctx, r.mysqlConn)
		if err != nil {
			return
		}

		if withCount == 0 {
			return
		}
	}

	if pageSize > 0 {
		if page > 1 {
			mods = append(mods, qm.Offset(pageSize*(page-1)))
		}
		mods = append(mods, qm.Limit(pageSize))
	}

	var columns []string
	if selectCol != "" {
		if selectCol != "," {
			columns = strings.Split(selectCol, ",")
		}
		columns = append(columns, model.BlogAttachmentRelationshipColumns.AttachmentRelationshipID)
		columns = append(columns, model.BlogAttachmentRelationshipColumns.ObjectID)
	}
	if len(columns) > 0 {
		mods = append(mods, qm.Select(columns...))
	}

	list, err = model.BlogAttachmentRelationships(mods...).All(ctx, r.mysqlConn)
	return
}
